/**
 * 
 */
package com.example.glboardtest1;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLUtils;


public class Board {
	
	private FloatBuffer vertexFieldBuffer;	// buffer holding the vertices
	
//	private FloatBuffer vertexPieceBuffer;	// buffer holding the vertices
//
//	private FloatBuffer textureBuffer;	// buffer holding the texture coordinates

	
	private ShortBuffer indicesBuffer;
	
	private int[] textures = new int[1];
	
	private float cursorX = 0.0f;
	private float cursorY = 0.0f;
	private float cursorZ = 0.0f;
	
	private int angle = 0;
	
	private Cursor cursor;
	
	float field_radius = 0.48f;
	
	private float fieldVertices[] = {  
			-field_radius, 0.0f, field_radius,
			field_radius, 0.0f, field_radius,
            -field_radius, 0.0f, -field_radius,
            field_radius, 0.0f, -field_radius
         };

	
//	private float pieceVertices[] = {  
//			-0.5f, 1.0f, 0.0f,
//            0.5f, 1.0f, 0.0f,
//            -0.5f, 0.0f, 0.0f,
//            0.5f, 0.0f, 0.0f
//         };

	
	private short indices[] ={ 
			0, 1, 2,
            2, 3, 1 
	};
	
//	private float texture[] = {    		
//			// Mapping coordinates for the vertices
//			0.0f, 1.0f,		// top left		(V2)
//			0.0f, 0.0f,		// bottom left	(V1)
//			1.0f, 1.0f,		// top right	(V4)
//			1.0f, 0.0f		// bottom right	(V3)
//	};
	
	public Board() {
		ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(fieldVertices.length * 4);
		vertexByteBuffer.order(ByteOrder.nativeOrder());
		vertexFieldBuffer = vertexByteBuffer.asFloatBuffer();		
		vertexFieldBuffer.put(fieldVertices);		
		vertexFieldBuffer.flip();
		
//		vertexByteBuffer = ByteBuffer.allocateDirect(pieceVertices.length * 4);
//		vertexByteBuffer.order(ByteOrder.nativeOrder());
//		vertexPieceBuffer = vertexByteBuffer.asFloatBuffer();		
//		vertexPieceBuffer.put(pieceVertices);		
//		vertexPieceBuffer.flip();
		
		vertexByteBuffer = ByteBuffer.allocateDirect(6 * 2);
		vertexByteBuffer.order(ByteOrder.nativeOrder());
        indicesBuffer = vertexByteBuffer.asShortBuffer();
        indicesBuffer.put(indices);
        indicesBuffer.flip();
        
//        vertexByteBuffer = ByteBuffer.allocateDirect(texture.length * 4);
//        vertexByteBuffer.order(ByteOrder.nativeOrder());
//		textureBuffer = vertexByteBuffer.asFloatBuffer();
//		textureBuffer.put(texture);
//		textureBuffer.position(0);
		
	}
 
//	public void loadGLTexture(GL10 gl, Context context) {
//		// loading texture
//		Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
//				R.drawable.ic_launcher);
//
//		// generate one texture pointer
//		gl.glGenTextures(1, textures, 0);
//		// ...and bind it to our array
//		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
//		
//		// create nearest filtered texture
//		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
//		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
//
//		//Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
////		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
////		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
//		
//		// Use Android GLUtils to specify a two-dimensional texture image from our bitmap 
//		GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
//		
//		// Clean up
//		bitmap.recycle();
//	}
	
	/** The draw method for the triangle with the GL context */
	public void draw(GL10 gl, Cursor cursor) {
		
		this.cursor =cursor;

		drawBoard(gl);
	}



	private void drawBoard(GL10 gl) {
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		
		gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
		
		gl.glVertexPointer( 3, GL10.GL_FLOAT, 0, vertexFieldBuffer);            

		float fieldSize = 1.0f;//not changeable!!!
		
		int xSize = 4;
		int zSize = 4;
		
		angle++;
		
		for(float posX = -xSize*fieldSize ; posX<=xSize*fieldSize ; posX= posX + fieldSize){
				for(float posZ = -zSize*fieldSize ; posZ<=zSize*fieldSize ; posZ= posZ + fieldSize){
//						float cosA = (float) Math.sin(-Math.PI*angle/180);
//						float sinA = (float) Math.cos(-Math.PI*angle/180);
//					cursor.moveCursor(posX*cosA -posZ*sinA, 0.0f, posX*sinA + posZ*cosA, gl);
					cursor.moveCursor(posX, 0.0f, posZ, gl);
					gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indicesBuffer);
				}			
		}
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}
	
	public void drawOnField(GL10 gl){
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer( 3, GL10.GL_FLOAT, 0, vertexFieldBuffer);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT, indicesBuffer);		
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
	
	
	public void clearCursor(){
		cursorX = 0.0f;
		cursorY = 0.0f;
		cursorZ = 0.0f;
	}
	
	public void moveCursor(float x, float y , float z, GL10 gl){		
		gl.glTranslatef(x - cursorX, y - cursorY, z -cursorZ);		
		cursorX = x;
		cursorY = y;
		cursorZ = z;
	}
	
	public void clearAngle(){
		angle = 0;
	}
	
	public void rotate(int v, GL10 gl){
		gl.glRotatef(v - angle, 0, 1, 0);
		angle = v;
	}
	
}
